package com.bxd.baodan.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
//public class LineLayout extends ViewGroup {
//	int heightMeasured = 0;
//	private final static int VIEW_MARGIN = 10;
//
//	public LineLayout(Context context, AttributeSet attrs) {
//		super(context, attrs);
//	}
//
//	public LineLayout(Context context) {
//		super(context);
//	}
//
//	@Override
//	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//		if(heightMeasureSpec == 0){
//			heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightMeasured, MeasureSpec.AT_MOST);
//			}
//		setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec), getDefaultSize(this.getSuggestedMinimumHeight(), heightMeasureSpec));
////		for (int index = 0; index < getChildCount(); index++) {
////			View child = getChildAt(index);
////			child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
//////			child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
////		}
////		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
////		final int count = getChildCount();
////		for (int i = 0; i < count; i++) {
////			getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
////		}
//	}
//	@Override
//	protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
//		final int count = getChildCount();
//		int row = 0;// which row lay you view relative to parent
//		int lengthX = arg1; // right position of child relative to parent
//		int lengthY = arg2; // bottom position of child relative to parent
//		for (int i = 0; i < count; i++) {
//			final View child = this.getChildAt(i);
//			int width = child.getMeasuredWidth();
//			int height = child.getMeasuredHeight();
//			lengthX += width + VIEW_MARGIN;
//			lengthY = row * (height + VIEW_MARGIN) + VIEW_MARGIN + height
//					+ arg2;
//			// if it can't drawing on a same line , skip to next line
//			if (lengthX > arg3) {
//				lengthX = width + VIEW_MARGIN + arg1;
//				row++;
//				lengthY = row * (height + VIEW_MARGIN) + VIEW_MARGIN + height
//						+ arg2;
//			}
////			child.measure(arg3 - arg1, arg4-arg2);
//			child.layout(lengthX - width, lengthY - height, lengthX, lengthY);
//		}
//	}
//}


/**
 * @author RAW
 */
public class LineLayout extends ViewGroup {
    private final static int PAD_H = 2, PAD_V = 2; // Space between child views.
    private int mHeight;

    public LineLayout(Context context) {
        super(context);
    }

    public LineLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        assert (MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.UNSPECIFIED);
        final int width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();
        int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom();
        final int count = getChildCount();
        int xpos = getPaddingLeft();
        int ypos = getPaddingTop();
        int childHeightMeasureSpec;
        if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST)
            childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
        else
            childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        mHeight = 0;
        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                child.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST), childHeightMeasureSpec);
                final int childw = child.getMeasuredWidth();
                mHeight = Math.max(mHeight, child.getMeasuredHeight() + PAD_V);
                if (xpos + childw > width) {
                    xpos = getPaddingLeft();
                    ypos += mHeight;
                }
                xpos += childw + PAD_H;
            }
        }
        if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.UNSPECIFIED) {
            height = ypos + mHeight;
        } else if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) {
            if (ypos + mHeight < height) {
                height = ypos + mHeight;
            }
        }
        height += 5; // Fudge to avoid clipping bottom of last row.
        setMeasuredDimension(width, height);
    } // end onMeasure()

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int width = r - l;
        int xpos = getPaddingLeft();
        int ypos = getPaddingTop();
        for (int i = 0; i < getChildCount(); i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                final int childw = child.getMeasuredWidth();
                final int childh = child.getMeasuredHeight();
                if (xpos + childw > width) {
                    xpos = getPaddingLeft();
                    ypos += mHeight;
                }
                child.layout(xpos, ypos, xpos + childw, ypos + childh);
                xpos += childw + PAD_H;
            }
        }
    } // end onLayout()
}
